<?php

namespace Addons\OaSystem\Controller;
use Think\ManageBaseController;

class OaCheckPointController extends OaBaseController
{
    protected $model;

    function _initialize()
    {
        $this->model = $this->getModel('oa_check_point');

        parent::_initialize();
        $controller = strtolower(CONTROLLER_NAME);
        $action = strtolower(ACTION_NAME);

        $res ['title'] = '考勤信息';
        $res ['url'] = addons_url('OaSystem://OaEmpAttendance/attendancelists', array('mdm' => I('mdm')));
        $res ['class'] = $action == 'attendancelists' ? 'cur' : '';
        $nav [] = $res;
        $this->assign('nav', $nav);

        $res ['title'] = '考勤列表';
        $sub_nav[] = $res;
        $res ['title'] = '加班列表';
        $res ['url'] = addons_url('OaSystem://OaEmpAttendance/overtimelists', array('mdm' => I('mdm')));
        $res ['class'] = $action == 'overtimelists' ? 'cur' : '';
        $sub_nav[] = $res;
        $res ['title'] = '请假列表';
        $res ['url'] = addons_url('OaSystem://OaEmpAttendance/leavelists', array('mdm' => I('mdm')));
        $res ['class'] = $action == 'leavelists' ? 'cur' : '';
        $sub_nav[] = $res;
        $res ['title'] = '考勤点管理';
        $res ['url'] = addons_url('OaSystem://OaCheckPoint/lists', array('mdm' => I('mdm')));
        $res ['class'] = $controller == 'oacheckpoint' ? 'cur' : '';
        $sub_nav[] = $res;
        $res ['title'] = '工作时间设置';
        $res ['url'] = addons_url('OaSystem://OaWorkTimeSetting/lists', array('mdm' => I('mdm')));
        $res ['class'] = $controller == 'oaworktimesetting' ? 'cur' : '';
        $sub_nav[] = $res;
        $this->assign('sub_nav', $sub_nav);
    }

    function lists()
    {
        $list_data = $this->_get_model_list($this->model);
        $grids = $list_data['list_grids'];
        //$data = $list_data['list_data'];
        $data = db_select('oa_check_point');
        foreach ($data as $i => $v) {
            $location = json_decode($v['location'], true);
            if($v['type'] == 'wifi')
                $location_str = '名称: &nbsp&nbsp&nbsp'.$location['SSID'].'<br>MAC: &nbsp&nbsp'.$location['BSSID'];
            else
                $location_str = '经度: &nbsp&nbsp'.$location['longitude'].'<br>纬度: &nbsp&nbsp'.$location['latitude'].'<br>半径: &nbsp&nbsp'.$location['radius'];
            $data[$i]['location'] = $location_str;

            $delUrl = U('del', array('id'=> $v['id']));
            $data[$i]['urls'] = "<a class='confirm' href=$delUrl>删除</a>";

        }
        $this->assign('list_grids', $grids);
        $this->assign('list_data', $data);
        $this->display();
    }

    function add()
    {
        if(IS_POST) {
            $data['appid'] = $this->appId;

            if($_POST['button_cancel']==1) {
                redirect(U('lists'));
            }

            if(empty($_POST['type']) || empty($_POST['ssid_longitude']) || empty($_POST['bssid_latitude'])) {
                $this->error('有参数未输入, 请检查后重新提交!');
                return false;
            }

            if($_POST['type'] == 'gps' && empty($_POST['gps_radius'])) {
                $this->error('请填写gps 半径!');
                return false;
            }
            $data['name'] = $_POST['name'];
            $data['type'] = $_POST['type'];
            if($data['type'] == 'wifi') {
                $location['SSID'] = $_POST['ssid_longitude'];
                $location['BSSID'] = $_POST['bssid_latitude'];
            } else {
                $location['longitude'] = $_POST['ssid_longitude'];
                $location['latitude'] = $_POST['bssid_latitude'];
                $location['radius'] = floatval($_POST['gps_radius']);
            }
            $data['location'] = json_encode($location, JSON_UNESCAPED_UNICODE);
            if(db_find('oa_check_point', $data)) {
                $this->error('该位置已经输入过了, 请检查后重新提交!');
                return false;
            }
            if(M('oa_check_point')->add($data) == false) {
                $this->error('添加位置信息失败!');
                return false;
            }
            $this->success ( '新增' . $model ['title'] . '成功！', U ( 'lists?model=' . $model ['name'], $this->get_param ));
        } else {
            $this->display();
        }
    }
}